package top.lcpsky.flashcards.util;

import java.util.*;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.*;


/**
 * 
 * <h2>kiftd配置解析器</h2>
 * <p>
 * 该工具负责读取并解析配置文件，并将结果随时提供给kiftd服务器业务逻辑以完成相应功能（例如用户认证、权限判定、配置启动端口等）。
 * </p>
 * 
 * @author 青阳龙野(kohgylw)
 * @version 1.0
 */
public class ConfigureReader {
	private static ConfigureReader cr;
	private Properties serverp;
	private int propertiesStatus;
	private String path;
	private String fileSystemPath;
	private String confdir;
	private int port;
	private String FSPath;
	private int bufferSize;
	private String fileBlockPath;
	private String fileNodePath;
	private String TFPath;

	private final String SERVER_PROPERTIES_FILE = "application-dev.properties";
	private final int DEFAULT_BUFFER_SIZE = 1048576;
	private final String DEFAULT_FILE_SYSTEM_PATH;
	private final String DEFAULT_FILE_SYSTEM_PATH_SETTING = "DEFAULT";

	public static final int INVALID_PORT = 1;
	public static final int INVALID_LOG = 2;
	public static final int INVALID_FILE_SYSTEM_PATH = 3;
	public static final int INVALID_BUFFER_SIZE = 4;
	public static final int CANT_CREATE_FILE_BLOCK_PATH = 5;
	public static final int CANT_CREATE_FILE_NODE_PATH = 6;
	public static final int CANT_CREATE_TF_PATH = 7;
	public static final int CANT_CONNECT_DB = 8;
	public static final int LEGAL_PROPERTIES = 0;

	private ConfigureReader() {
		this.propertiesStatus = -1;
		 ClassPathResource resource = new ClassPathResource(SERVER_PROPERTIES_FILE);
         try {
        	 serverp = PropertiesLoaderUtils.loadProperties(resource);
        	 this.propertiesStatus = this.testServerPropertiesAndEffect();
        	 if (this.propertiesStatus == LEGAL_PROPERTIES) {
        		 System.out.println("准备就绪。");
        	 }
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		this.path = serverp.getProperty("FS.path");// 开发环境下
		this.DEFAULT_FILE_SYSTEM_PATH = this.path + File.separator + "filesystem" + File.separator;
	}

	public static ConfigureReader instance() {
		if (ConfigureReader.cr == null) {
			ConfigureReader.cr = new ConfigureReader();
		}
		return ConfigureReader.cr;
	}


	public int getBuffSize() {
		return this.bufferSize;
	}

	

	public int getPort() {
		return this.port;
	}

	public int getPropertiesStatus() {
		return this.propertiesStatus;
	}



	/**
	 * 
	 * <h2>检查是否开启了mysql数据库</h2>
	 * <p>
	 * 用于检查是否使用自定义的外部MySQL数据库。
	 * </p>
	 * 
	 * @author 青阳龙野(kohgylw)
	 * @return boolean 是否使用了外部MySQL数据库
	 */
	public boolean useMySQL() {
		return true;
	}



	private int testServerPropertiesAndEffect() {
		final String bufferSizes = this.serverp.getProperty("buff.size");
		if (bufferSizes == null) {
			// Printer.instance.print("警告：未找到缓冲大小配置，将采用默认值（1048576）。");
			this.bufferSize = 1048576;
		} else {
			try {
				this.bufferSize = Integer.parseInt(bufferSizes);
				if (this.bufferSize <= 0) {
					// Printer.instance.print("错误：缓冲区大小设置无效。");
					return 4;
				}
			} catch (Exception e2) {
				// Printer.instance.print("错误：缓冲区大小设置无效。");
				return 4;
			}
		}
		this.FSPath = this.serverp.getProperty("FS.path");
		if (this.FSPath == null) {
			// Printer.instance.print("警告：未找到文件系统配置，将采用默认值。");
			this.fileSystemPath = this.DEFAULT_FILE_SYSTEM_PATH;
		} else if (this.FSPath.equals("DEFAULT")) {
			this.fileSystemPath = this.DEFAULT_FILE_SYSTEM_PATH;
		} else {
			this.fileSystemPath = this.FSPath;
		}
		if (!fileSystemPath.endsWith(File.separator)) {
			fileSystemPath = fileSystemPath + File.separator;
		}
		final File fsFile = new File(this.fileSystemPath);
		if (!fsFile.isDirectory() || !fsFile.canRead() || !fsFile.canWrite()) {
			// Printer.instance.print("错误：文件系统路径[" + this.fileSystemPath + "]无效，该路径必须指向一个具备读写权限的文件夹。");
			return 3;
		}
		this.fileBlockPath = this.fileSystemPath + "fileblocks" + File.separator;
		final File fbFile = new File(this.fileBlockPath);
		if (!fbFile.isDirectory() && !fbFile.mkdirs()) {
			// Printer.instance.print("错误：无法创建文件块存放区[" + this.fileBlockPath + "]。");
			return 5;
		}
		this.fileNodePath = this.fileSystemPath + "filenodes" + File.separator;
		final File fnFile = new File(this.fileNodePath);
		if (!fnFile.isDirectory() && !fnFile.mkdirs()) {
			// Printer.instance.print("错误：无法创建文件节点存放区[" + this.fileNodePath + "]。");
			return 6;
		}
		this.TFPath = this.fileSystemPath + "temporaryfiles" + File.separator;
		final File tfFile = new File(this.TFPath);
		if (!tfFile.isDirectory() && !tfFile.mkdirs()) {
			// Printer.instance.print("错误：无法创建临时文件存放区[" + this.TFPath + "]。");
			return 7;
		}
		// Printer.instance.print("检查完毕。");
		return 0;
	}

	public String getFileBlockPath() {
		return this.fileBlockPath;
	}

	public String getTemporaryfilePath() {
		return this.TFPath;
	}


	public String getFileSystemPath() {
		return this.fileSystemPath;
	}



}
